package com.example.kitchen;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;
import java.util.logging.LogRecord;

import broadcast.KitchenReceiver;
import service.KitchenService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,KitchenReceiver.Message{

    TextView text_order_info;
    Button btn_kitchen_end;
    Button btn_kitchen_begin;
    KitchenReceiver kitchenReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        动态注册广播
        kitchenReceiver = new KitchenReceiver();
        // 因为这里需要注入Message，所以不能在AndroidManifest文件中静态注册广播接收器
        kitchenReceiver.setMessage(this);
        IntentFilter intentFilter = new IntentFilter();
//        设置广播接收类型
        intentFilter.addAction("com.example.order");
//        动态注册
        registerReceiver(kitchenReceiver,intentFilter);

        text_order_info = findViewById(R.id.text_order_info);

        btn_kitchen_end = findViewById(R.id.btn_kitchen_end);
        btn_kitchen_end.setOnClickListener(this);

        btn_kitchen_begin = findViewById(R.id.btn_kitchen_begin);
        btn_kitchen_begin.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(kitchenReceiver);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_kitchen_begin:{
                Intent intent = new Intent(this, KitchenService.class);
                startService(intent);
            }
            break;
            case R.id.btn_kitchen_end:{
                Intent intent = new Intent(this, KitchenService.class);
                stopService(intent);
            }
            break;
        }
    }

    @Override
    public void getMsg(String str) {
        text_order_info.append(str);
    }
}